/*     */ package com.zimbra.cs.db;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.store.MailboxBlob.MailboxBlobInfo;
/*     */ import com.zimbra.cs.store.file.BlobReference;
/*     */ import com.zimbra.cs.store.file.FileBlobStore;
/*     */ import com.zimbra.cs.util.SpoolingCache;
/*     */ import com.zimbra.cs.volume.Volume;
/*     */ import java.io.IOException;
/*     */ import java.sql.PreparedStatement;
/*     */ import java.sql.ResultSet;
/*     */ import java.sql.SQLException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class DbVolumeBlobs
/*     */ {
/*     */   private static final String TB_VOLUME_BLOBS = "volume_blobs";
/*     */   private static final String CN_ID = "id";
/*     */   private static final String CN_VOLUME_ID = "volume_id";
/*     */   private static final String CN_MAILBOX_ID = "mailbox_id";
/*     */   private static final String CN_ITEM_ID = "item_id";
/*     */   private static final String CN_REVISION = "revision";
/*     */   private static final String CN_DIGEST = "blob_digest";
/*     */   private static final String CN_PROCESSED = "processed";
/*     */   private static final String SELECT_BLOB_REFS = "SELECT id,volume_id,mailbox_id,item_id,revision,blob_digest,processed FROM volume_blobs WHERE ";
/*     */   private static final String DELETE_BLOB_REFS = "DELETE FROM volume_blobs WHERE ";
/*     */   
/*     */   public static void addBlobReference(DbPool.DbConnection conn, Mailbox mbox, Volume vol, MailItem item)
/*     */     throws ServiceException
/*     */   {
/*  53 */     PreparedStatement stmt = null;
/*     */     try {
/*  55 */       stmt = conn.prepareStatement("INSERT INTO volume_blobs (volume_id, mailbox_id, item_id, revision,blob_digest,processed) VALUES (?, ?, ?, ?, ?, ?)");
/*     */       
/*     */ 
/*  58 */       int pos = 1;
/*  59 */       stmt.setShort(pos++, vol.getId());
/*  60 */       stmt.setInt(pos++, mbox.getId());
/*  61 */       stmt.setInt(pos++, item.getId());
/*  62 */       stmt.setInt(pos++, item.getSavedSequence());
/*  63 */       stmt.setString(pos++, item.getDigest());
/*  64 */       stmt.setBoolean(pos++, false);
/*  65 */       stmt.executeUpdate();
/*     */     } catch (SQLException e) {
/*  67 */       throw ServiceException.FAILURE("unable to insert new blob reference for item " + item, e);
/*     */     } finally {
/*  69 */       DbPool.closeStatement(stmt);
/*     */     }
/*     */   }
/*     */   
/*     */   public static void addBlobReference(DbPool.DbConnection conn, MailboxBlob.MailboxBlobInfo info) throws ServiceException {
/*  74 */     short volId = Short.valueOf(info.locator).shortValue();
/*  75 */     String path = FileBlobStore.getBlobPath(info.mailboxId, info.itemId, info.revision, volId);
/*  76 */     PreparedStatement stmt = null;
/*     */     try {
/*  78 */       stmt = conn.prepareStatement("INSERT INTO volume_blobs (volume_id, mailbox_id, item_id, revision,blob_digest,processed) VALUES (?, ?, ?, ?, ?, ?)");
/*     */       
/*     */ 
/*  81 */       int pos = 1;
/*  82 */       stmt.setShort(pos++, volId);
/*  83 */       stmt.setInt(pos++, info.mailboxId);
/*  84 */       stmt.setInt(pos++, info.itemId);
/*  85 */       stmt.setInt(pos++, info.revision);
/*  86 */       stmt.setString(pos++, info.digest);
/*  87 */       stmt.setBoolean(pos++, false);
/*  88 */       stmt.executeUpdate();
/*     */     } catch (SQLException e) {
/*  90 */       if (Db.errorMatches(e, Db.Error.DUPLICATE_ROW)) {
/*  91 */         throw MailServiceException.ALREADY_EXISTS(path, e);
/*     */       }
/*  93 */       throw ServiceException.FAILURE("unable to insert new blob reference for path " + path, e);
/*     */     }
/*     */     finally {
/*  96 */       DbPool.closeStatement(stmt);
/*     */     }
/*     */   }
/*     */   
/*     */   public static void updateProcessed(DbPool.DbConnection conn, long id, boolean processed) throws ServiceException {
/* 101 */     PreparedStatement stmt = null;
/*     */     try {
/* 103 */       stmt = conn.prepareStatement("UPDATE volume_blobs SET processed = ? WHERE id = ?");
/* 104 */       int pos = 1;
/* 105 */       stmt.setBoolean(pos++, processed);
/* 106 */       stmt.setLong(pos++, id);
/* 107 */       stmt.executeUpdate();
/*     */     } catch (SQLException e) {
/* 109 */       throw ServiceException.FAILURE("unable to update processed", e);
/*     */     } finally {
/* 111 */       DbPool.closeStatement(stmt);
/*     */     }
/*     */   }
/*     */   
/*     */   public static List<BlobReference> getBlobReferences(DbPool.DbConnection conn, String digest, Volume volume) throws ServiceException {
/* 116 */     PreparedStatement stmt = null;
/* 117 */     ResultSet rs = null;
/*     */     try {
/* 119 */       stmt = conn.prepareStatement("SELECT id,volume_id,mailbox_id,item_id,revision,blob_digest,processed FROM volume_blobs WHERE blob_digest = ? AND volume_id = ? ORDER BY blob_digest ASC");
/* 120 */       int pos = 1;
/* 121 */       stmt.setString(pos++, digest);
/* 122 */       stmt.setShort(pos++, volume.getId());
/* 123 */       rs = stmt.executeQuery();
/* 124 */       return fillBlobReferences(rs);
/*     */     } catch (SQLException e) {
/* 126 */       throw ServiceException.FAILURE("unable to query blob references", e);
/*     */     } finally {
/* 128 */       DbPool.closeResults(rs);
/* 129 */       DbPool.closeStatement(stmt);
/*     */     }
/*     */   }
/*     */   
/*     */   private static List<BlobReference> fillBlobReferences(ResultSet rs) throws SQLException {
/* 134 */     List<BlobReference> blobs = new ArrayList();
/* 135 */     while (rs.next()) {
/* 136 */       BlobReference blob = new BlobReference();
/* 137 */       blob.setId(rs.getLong(1));
/* 138 */       blob.setVolumeId(rs.getShort(2));
/* 139 */       blob.setMailboxId(rs.getInt(3));
/* 140 */       blob.setItemId(rs.getInt(4));
/* 141 */       blob.setRevision(rs.getInt(5));
/* 142 */       blob.setDigest(rs.getString(6));
/* 143 */       blob.setProcessed(rs.getBoolean(7));
/* 144 */       blobs.add(blob);
/*     */     }
/* 146 */     return blobs;
/*     */   }
/*     */   
/*     */   private static SpoolingCache<String> fillBlobDigests(ResultSet rs) throws SQLException, IOException {
/* 150 */     SpoolingCache<String> digests = new SpoolingCache();
/* 151 */     while (rs.next()) {
/* 152 */       digests.add(rs.getString(1));
/*     */     }
/* 154 */     return digests;
/*     */   }
/*     */   
/*     */   public static List<BlobReference> getBlobReferences(DbPool.DbConnection conn, Volume vol) throws ServiceException {
/* 158 */     PreparedStatement stmt = null;
/* 159 */     ResultSet rs = null;
/*     */     try {
/* 161 */       stmt = conn.prepareStatement("SELECT id,volume_id,mailbox_id,item_id,revision,blob_digest,processed FROM volume_blobs WHERE volume_id = ? ORDER BY blob_digest ASC");
/* 162 */       int pos = 1;
/* 163 */       stmt.setShort(pos++, vol.getId());
/* 164 */       rs = stmt.executeQuery();
/* 165 */       return fillBlobReferences(rs);
/*     */     } catch (SQLException e) {
/* 167 */       throw ServiceException.FAILURE("unable to query blob references", e);
/*     */     } finally {
/* 169 */       DbPool.closeResults(rs);
/* 170 */       DbPool.closeStatement(stmt);
/*     */     }
/*     */   }
/*     */   
/*     */   public static SpoolingCache<String> getUniqueDigests(DbPool.DbConnection conn, Volume vol) throws ServiceException, IOException {
/* 175 */     PreparedStatement stmt = null;
/* 176 */     ResultSet rs = null;
/*     */     try {
/* 178 */       stmt = conn.prepareStatement("SELECT DISTINCT blob_digest FROM volume_blobs WHERE volume_id = ? AND processed = ? ");
/*     */       
/* 180 */       int pos = 1;
/* 181 */       stmt.setShort(pos++, vol.getId());
/* 182 */       stmt.setBoolean(pos++, false);
/* 183 */       rs = stmt.executeQuery();
/* 184 */       return fillBlobDigests(rs);
/*     */     } catch (SQLException e) {
/* 186 */       throw ServiceException.FAILURE("unable to query blob references", e);
/*     */     } finally {
/* 188 */       DbPool.closeResults(rs);
/* 189 */       DbPool.closeStatement(stmt);
/*     */     }
/*     */   }
/*     */   
/*     */   public static void deleteBlobRef(DbPool.DbConnection conn, long id) throws ServiceException {
/* 194 */     PreparedStatement stmt = null;
/* 195 */     ResultSet rs = null;
/*     */     try {
/* 197 */       stmt = conn.prepareStatement("DELETE FROM volume_blobs WHERE id = ?");
/* 198 */       int pos = 1;
/* 199 */       stmt.setLong(pos++, id);
/* 200 */       stmt.executeUpdate();
/*     */     } catch (SQLException e) {
/* 202 */       throw ServiceException.FAILURE("unable to delete blob references", e);
/*     */     } finally {
/* 204 */       DbPool.closeResults(rs);
/* 205 */       DbPool.closeStatement(stmt);
/*     */     }
/*     */   }
/*     */   
/*     */   public static void deleteBlobRef(DbPool.DbConnection conn, Mailbox mbox) throws ServiceException {
/* 210 */     PreparedStatement stmt = null;
/* 211 */     ResultSet rs = null;
/*     */     try {
/* 213 */       stmt = conn.prepareStatement("DELETE FROM volume_blobs WHERE mailbox_id = ?");
/* 214 */       int pos = 1;
/* 215 */       stmt.setInt(pos++, mbox.getId());
/* 216 */       stmt.executeUpdate();
/*     */     } catch (SQLException e) {
/* 218 */       throw ServiceException.FAILURE("unable to delete blob references", e);
/*     */     } finally {
/* 220 */       DbPool.closeResults(rs);
/* 221 */       DbPool.closeStatement(stmt);
/*     */     }
/*     */   }
/*     */   
/*     */   public static void deleteBlobRef(DbPool.DbConnection conn, Volume vol) throws ServiceException {
/* 226 */     PreparedStatement stmt = null;
/* 227 */     ResultSet rs = null;
/*     */     try {
/* 229 */       stmt = conn.prepareStatement("DELETE FROM volume_blobs WHERE volume_id = ?");
/* 230 */       int pos = 1;
/* 231 */       stmt.setShort(pos++, vol.getId());
/* 232 */       stmt.executeUpdate();
/*     */     } catch (SQLException e) {
/* 234 */       throw ServiceException.FAILURE("unable to delete blob references", e);
/*     */     } finally {
/* 236 */       DbPool.closeResults(rs);
/* 237 */       DbPool.closeStatement(stmt);
/*     */     }
/*     */   }
/*     */   
/*     */   public static void deleteAllBlobRef(DbPool.DbConnection conn) throws ServiceException {
/* 242 */     PreparedStatement stmt = null;
/*     */     try {
/* 244 */       stmt = conn.prepareStatement("TRUNCATE TABLE volume_blobs");
/* 245 */       stmt.executeUpdate();
/*     */     } catch (SQLException e) {
/* 247 */       throw ServiceException.FAILURE("unable to delete blob references", e);
/*     */     } finally {
/* 249 */       DbPool.closeStatement(stmt);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/db/DbVolumeBlobs.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */